﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SuppKids.Core.Extensions
{
    public static class TExtensions
	{
		public static TResult With<TInput, TResult>(this TInput o, Func<TInput, TResult> evaluator)
			where TInput : class
		{
			if (o == null)
			{
				return default(TResult);
			}

			return evaluator(o);
		}

		public static TResult Return<TInput, TResult>(this TInput o, Func<TInput, TResult> evaluator, TResult failureValue)
			where TInput : class
		{
			if (o == null)
			{
				return failureValue;
			}

			return evaluator(o);
		}

		public static TInput If<TInput>(this TInput o, Func<TInput, bool> evaluator) where TInput : class
		{
			if (o == null)
			{
				return null;
			}

			return evaluator(o) ? o : null;
		}

		public static IEnumerable<TFirst> Except<TFirst, TSecond>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, bool> comparer)
		{
			return first.Where(f => !second.Any(s => comparer(f, s)));
		}
	}
}
